package com.ruoyi.web.controller.system;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PictureComment;
import com.ruoyi.system.domain.PictureInfo;
import com.ruoyi.system.service.IPictureCommentService;
import com.ruoyi.system.service.IPictureInfoService;

/**
 * 图片评论Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/picture/comment")
public class PictureCommentController extends BaseController
{
    @Autowired
    private IPictureCommentService pictureCommentService;
    
    @Autowired
    private IPictureInfoService pictureInfoService;

    /**
     * 查询图片评论列表
     */
    @GetMapping("/list")
    public TableDataInfo list(PictureComment pictureComment)
    {
        startPage();
        List<PictureComment> list = pictureCommentService.selectPictureCommentList(pictureComment);
        return getDataTable(list);
    }

    /**
     * 根据图片ID获取评论列表（按时间倒序）
     */
    @GetMapping("/picture/{pictureId}")
    public AjaxResult getCommentsByPictureId(@PathVariable Long pictureId)
    {
        List<PictureComment> list = pictureCommentService.selectPictureCommentsByPictureId(pictureId);
        return AjaxResult.success(list);
    }

    /**
     * 新增图片评论
     */
    @Log(title = "图片评论", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PictureComment pictureComment)
    {
        if (pictureComment.getPictureId() == null)
        {
            return AjaxResult.error("图片ID不能为空");
        }
        if (pictureComment.getContent() == null || pictureComment.getContent().trim().isEmpty())
        {
            return AjaxResult.error("评论内容不能为空");
        }
        
        // 插入评论
        int result = pictureCommentService.insertPictureComment(pictureComment);
        
        // 更新图片的remark字段为1（已评论）
        if (result > 0)
        {
            PictureInfo pictureInfo = new PictureInfo();
            pictureInfo.setId(pictureComment.getPictureId());
            pictureInfo.setRemark(1); // 设置为已评论
            pictureInfoService.updatePictureInfo(pictureInfo);
        }
        
        return toAjax(result);
    }

    /**
     * 修改图片评论
     */
    @Log(title = "图片评论", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PictureComment pictureComment)
    {
        return toAjax(pictureCommentService.updatePictureComment(pictureComment));
    }

    /**
     * 删除图片评论
     */
    @Log(title = "图片评论", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds)
    {
        return toAjax(pictureCommentService.deletePictureCommentByCommentIds(commentIds));
    }
}